package com.deer.wms.base.system.util;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**读取Excel
 * @author 李帅辰
 * @version 1.0
 * @date 2021/1/18 10:25
 */
public class ExcelReadUtil {


  /*
   *
   * @param args
   * @author 李帅辰
   * @date 2021/1/18 10:25
   * @return void
   */
  public Map<String,List<String>> read(int fristIndex,int lastIndex,String path) throws IOException {


    List<String> mapList=new ArrayList<>();//存放地图坐标
    List<String> cellList = new ArrayList<>();//货位编码
    Map<String,List<String>> excelMap = new HashMap<>();
    //获取文件
    XSSFWorkbook workbook = new XSSFWorkbook(path);
    //获取文件的sheet
    for (int i = 0; i <=fristIndex; i++) {
      XSSFSheet sheetAt = workbook.getSheetAt(fristIndex);
      //遍历文件，获取行
      for (Row cells : sheetAt) {
        //获取当前行的单元格
        for (Cell cell : cells) {
          //获取单元格的内容
          cell.setCellType(CellType.STRING);//设置获取的单元格内容格式
          String value = cell.getStringCellValue();//获取单元格内容
          if(fristIndex%2==0){
            mapList.add(value);
          }else {
            cellList.add(value);
          }
        }
      }
      fristIndex++;
      if (fristIndex==lastIndex){
        break;
      }
    }

    //释放资源
    workbook.close();
    excelMap.put("cellList",cellList);
    excelMap.put("mapList",mapList);
    return excelMap;
  }

}
